{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f1c1f3f0",
   "metadata": {},
   "source": [
    "# Numpy数组操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3ef2d827",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "769ae00d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ecdd904",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.  ,  1.45,  1.9 ,  2.35,  2.8 ,  3.25,  3.7 ,  4.15,  4.6 ,\n",
       "        5.05,  5.5 ,  5.95,  6.4 ,  6.85,  7.3 ,  7.75,  8.2 ,  8.65,\n",
       "        9.1 ,  9.55, 10.  ])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,num=21)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "429759d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.arange(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5971fa58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e6bdafdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "673847e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty((2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaf2f8cb",
   "metadata": {},
   "source": [
    "# 随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40bf03c4",
   "metadata": {},
   "source": [
    "生成2行4列的随机整数。生成1行3列的随机数数组，其三个随机数的low都是1，upper分别是【3，5，10】."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "75c4c78a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  2,  2,  7],\n",
       "       [ 8,  6,  4, 10]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,11,(2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bc8f3bcf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(1,[3,5,10])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50eb6268",
   "metadata": {},
   "source": [
    ".生成9个元素的一维数组，使用reshape进行（3，3）的重排，并使用shuffle打乱。并将其用正态分布的随机数替代原本元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "30762494",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(9).reshape(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2a0d8e1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4418de65",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7ab59fad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 5],\n",
       "       [6, 7, 8],\n",
       "       [0, 1, 2]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "278c1937",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.62834858,  5.56333538,  5.23395827],\n",
       "       [ 6.67664015,  7.29867139,  8.29586484],\n",
       "       [-0.52802382,  2.35681222,  1.31797382]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "903c6508",
   "metadata": {},
   "source": [
    "生成16个元素的一维数组，整形为4*4的二维数组，输出（5，6，9，10）这四个元素，保持其本来形状。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "afa2dd37",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.arange(16).reshape(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "054c7354",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  6],\n",
       "       [ 9, 10]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[1:3,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9db75c75",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
